//
//  BSAddView.m
//  BaiSiBuDeJie
//
//  Created by 王位 on 8/24/16.
//  Copyright © 2016 tsaievan. All rights reserved.
//





//
//  BSAddViewController.m
//  BaiSiBuDeJie
//
//  Created by 蔡一凡 on 8/22/16.
//  Copyright © 2016 tsaievan. All rights reserved.
//

#import "BSAddViewController.h"

#import "BSAddBtn.h"

#import "BSAddBtnModel.h"

#import "UIView+HM.h"

#import "BSAddView.h"





@interface BSAddView ()


@property(nonatomic,strong)NSArray * dataArr;

@property(nonatomic,assign)NSInteger mark;

@property(nonatomic,strong)NSMutableArray * btnArr;

@property(nonatomic,weak)UIButton * cancelBtn;

@property(nonatomic,weak)UIToolbar * toolbar;

@end

@implementation BSAddView

+(instancetype)BSAddView
{
    BSAddView * addView = [BSAddView new];

    return addView;
}

-(void)layoutSubviews
{
        [self mixUp];
    
    [self addBtns];
    
    [self removeBtn];

}


//添加6个btn
- (void)addBtns
{
    
    NSInteger margin = 20;
    
    NSInteger width = 70;
    
    NSInteger height = 100;
    
    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"app_slogan"]];
    
    imageView.center = self.center;
    
    
    imageView.y = 10;
    
    [self addSubview:imageView];
    
    
    
    for(int i = 0; i < self.dataArr.count ; i++)
    {
        //行索引,列索引
        
        BSAddBtnModel * model = self.dataArr[i];
        
        NSInteger row =  i / 3;
        
        NSInteger los = i % 3;
        
        //间距
        NSInteger spacing = (self.w - 3 * width - 2 * margin) / 2;
        
        BSAddBtn * btn = [BSAddBtn BSAddBtnWithIcon:model.icon WithName:model.name];
        
        //        btn.backgroundColor = [UIColor redColor];
        
        
        btn.frame = CGRectMake( margin + los * (width + spacing) , row * (20 + height), width, height);
        
        
        btn.imageView.layer.cornerRadius = btn.w * 0.5;
        
        btn.imageView.layer.masksToBounds = YES;
        
        [self addSubview:btn];
        
        btn.tag = i;
        
        [self.btnArr addObject:btn];
        
        double time = (self.dataArr.count - btn.tag) * 0.15;
        
        if(self.dataArr.count - 2 == i)
        {
            time = 0.2;
        }
        
        if(self.dataArr.count - 1 == i)
        {
            time += 0.2;
        }
        
        
        NSLog(@"%lf",time);
        
        [UIView animateWithDuration:time animations:^{
            
            btn.y += 200;
        }];
        
        //给btn添加点击事件
        
        [btn addTarget:self action:@selector(didClicBtn:) forControlEvents:UIControlEventTouchDown];
        
    }
    
    
    [UIView animateWithDuration:1 animations:^{
        
        imageView.y += 100;
    }];
    
    [self.btnArr addObject:imageView];
    
}

//btn的点击事件

-(void)didClicBtn:(BSAddBtn*)btn
{
    if(self.delegate)
    {
         [self.delegate BSAddViewDelegateWith:btn.tag];
    }
    
    [self didClicCancelBtn:self.cancelBtn];
    
}

- (void)removeBtn
{
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.h - 44, self.w, 44)];
    
    self.cancelBtn = btn;
    
//    btn.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:btn];
    
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(didClicCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
    [self bringSubviewToFront:btn];
    
}

-(void)didClicCancelBtn:(UIButton*)btn
{
    btn.alpha = 0;
    
    //    NSLog(@"%@",self.btnArr);
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    for (int i = 0; i < self.btnArr.count; i++) {
        
        
        BSAddBtn * btn = self.btnArr[i];
        

        
        double time = (self.btnArr.count - btn.tag) * 0.12;
        
        if(self.dataArr.count - 2 == i)
        {
            time = 0.2;
        }
        
        if(self.dataArr.count - 1 == i)
        {
            time += 0.2;
        }
        
        [UIView animateWithDuration:time animations:^{
            
            btn.y = self.h;
        }];
        
        if(i == self.btnArr.count - 1)
        {
            break;
            
        }
        
    }
    
    UIImageView * imageView = self.btnArr.lastObject;
    
    [UIView animateWithDuration:1.0 animations:^{
        
        imageView.y = self.h;
        
    } completion:^(BOOL finished) {
        
        NSInteger time = 0.2 * (self.btnArr.count - 1);
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self removeAddView];
            
            [self.cancelBtn removeFromSuperview];
            
        });
        
        
        
    }];
    
    
}



- (void)removeAddView
{
    [self removeFromSuperview];
}


#pragma mark - 1.1懒加载

-(NSArray *)dataArr
{
    if(_dataArr == nil)
    {
        _dataArr = [BSAddBtnModel  dataArr];
    }
    
    return _dataArr;
}

-(NSMutableArray *)btnArr
{
    if(!_btnArr)
    {
        _btnArr = [[NSMutableArray alloc]init];
    }
    
    return _btnArr;
}

-(void)mixUp
{
    UIBlurEffect * effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    
    UIVisualEffectView * visual = [[UIVisualEffectView alloc]initWithEffect:effect];
    
    visual.frame = self.bounds;
    
    [self addSubview:visual];
    
}


@end




